package com.qh.resourcehub.program.wifi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qh.resourcehub.program.dto.VirtualDto;
import com.qh.resourcehub.program.dto.WifiDto;
import com.qh.resourcehub.program.entity.ApproveProcessDO;
import com.qh.resourcehub.program.entity.ApproveProcessRecordDO;
import com.qh.resourcehub.program.entity.VirtualDO;
import com.qh.resourcehub.program.entity.WifiDO;
import com.qh.resourcehub.program.mapper.ApproveProcessMapper;
import com.qh.resourcehub.program.mapper.ApproveProcessRecordMapper;
import com.qh.resourcehub.program.mapper.VirtualMapper;
import com.qh.resourcehub.program.mapper.WifiMapper;
import com.qh.resourcehub.program.virtual.service.VirtualService;
import com.qh.resourcehub.program.vo.VirtualVo;
import com.qh.resourcehub.program.vo.WifiVo;
import com.qh.resourcehub.program.wifi.service.WifiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WifiServiceImpl implements WifiService {
    @Autowired
    private WifiMapper wifiMapper;
    @Autowired
    private ApproveProcessMapper approveProcessMapper;

    @Autowired
    private ApproveProcessRecordMapper approveProcessRecordMapper;
    @Override
    public int ApplicationSave(WifiDto wifiDto) {
        int result = 0;
        if (!StringUtils.isEmpty(wifiDto.getApplyUser()) && StringUtils.isEmpty(wifiDto.getInfoId())){
            wifiDto.setInfoId(UUID.randomUUID().toString().replace("-", ""));
            //新增流程表
            ApproveProcessDO approveProcessDO=new ApproveProcessDO();
            approveProcessDO.setInfoId(wifiDto.getInfoId());//信息id
            approveProcessDO.setProcessStatus("00");//待提交
            approveProcessDO.setProcessTime(new Date());
            approveProcessDO.setProcessUser(wifiDto.getApplyUser());
            approveProcessDO.setInfoType("2");
            approveProcessDO.setProcessId(UUID.randomUUID().toString().replace("-", ""));
            approveProcessDO.setProcessRecordId(UUID.randomUUID().toString().replace("-", ""));
            approveProcessMapper.insert(approveProcessDO);
            WifiDO wifiDO=new WifiDO();
            BeanUtils.copyProperties(wifiDto, wifiDO);
            //新增基础表
            result=wifiMapper.insert(wifiDO);

        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ApplicationUpdata(WifiDto wifiDto) {
        int result = 0;
        //设置状态
        if (!StringUtils.isEmpty(wifiDto.getApplyUser()) && !StringUtils.isEmpty(wifiDto.getInfoId())){
            //新增流程表
            WifiDO wifiDO=new WifiDO();
            BeanUtils.copyProperties(wifiDto, wifiDO);
            //新增基础表
            LambdaQueryWrapper<WifiDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WifiDO::getInfoId,wifiDto.getInfoId());
            result=wifiMapper.update(wifiDO,queryWrapper);
        }
        if (!ObjectUtil.isEmpty(wifiDto.getProcessStatus()) && wifiDto.getProcessStatus().equals("010")) {
            //修改流程表状态为00待提交
            LambdaUpdateWrapper<ApproveProcessDO> updateWrapper=new LambdaUpdateWrapper<>();
            updateWrapper.eq(ApproveProcessDO::getProcessId,wifiDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "00");
            int updateCount =approveProcessMapper.update(null,updateWrapper);
            //并且删除对应流程信息表信息
            LambdaQueryWrapper<ApproveProcessRecordDO> deleteWrapper = new LambdaQueryWrapper<>();
            deleteWrapper.eq(ApproveProcessRecordDO::getRecordId, wifiDto.getProcessId());
            int de=approveProcessRecordMapper.delete(deleteWrapper);
            return (updateCount > 0 || de > 0) ? 1 : 0;
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int Applicationdel(WifiDto wifiDto) {
        int result = 0;
        //设置申请时间
        if (!StringUtils.isEmpty(wifiDto.getInfoId())){
            //删除流程表
            LambdaQueryWrapper<ApproveProcessDO> queryWrappers = new LambdaQueryWrapper<>();
            queryWrappers.eq(ApproveProcessDO::getInfoId,wifiDto.getInfoId());
            int updateCount =approveProcessMapper.delete(queryWrappers);
            //删除基础表
            LambdaQueryWrapper<WifiDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WifiDO::getInfoId,wifiDto.getInfoId());
            int deleteCount=wifiMapper.delete(queryWrapper);
            return  (updateCount > 0 || deleteCount > 0) ? 1 : 0;
        }
        return result;
    }

    @Override
    public List<WifiVo> queryWifiData(WifiDto wifiDto) {
        List<WifiVo> list=new ArrayList<>();
        wifiDto.setInfoType("2");
        if (!StringUtils.isEmpty(wifiDto.getUserCode())){
            wifiDto.setOffset(wifiDto.getOffsetNum());
             list = wifiMapper.queryWifiData(wifiDto);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ApplicationSubmit(WifiDto wifiDto) {
        int result = 0;
        //当前审批id和选择的审批用户id必传
        if (!StringUtils.isEmpty(wifiDto.getExamineUserId()) && !StringUtils.isEmpty(wifiDto.getProcessId())){
            //修改流程表
            LambdaQueryWrapper<ApproveProcessDO> queryWrappers = new LambdaQueryWrapper<>();
            queryWrappers.eq(ApproveProcessDO::getProcessId,wifiDto.getProcessId());
            ApproveProcessDO approveProcessDO=new ApproveProcessDO();
            approveProcessDO.setProcessStatus("01");//待审核
            //修改审批表对应流程状态
            int updateCount =approveProcessMapper.update(approveProcessDO,queryWrappers);
            //新增审批表流程信息表
            ApproveProcessRecordDO approveProcessRecordDO=new ApproveProcessRecordDO();
            approveProcessRecordDO.setProcessStatus("01");//待审核第一步
            approveProcessRecordDO.setExamineUserId(wifiDto.getExamineUserId());//审批用户id
            approveProcessRecordDO.setRecordId(wifiDto.getProcessId());
            int insrtCount  = approveProcessRecordMapper.insert(approveProcessRecordDO);
            return (updateCount > 0 || insrtCount > 0) ? 1 : 0;
        }
        return result;
    }

    @Override
    public int ApplicationWithdraw(WifiDto wifiDto) {
        int result = 0;
        if (!StringUtils.isEmpty(wifiDto.getProcessStatus()) && !StringUtils.isEmpty(wifiDto.getProcessId())){
            //修改当前流程为待提交
            LambdaUpdateWrapper<ApproveProcessDO> updateWrapper=new LambdaUpdateWrapper<>();
            updateWrapper.eq(ApproveProcessDO::getProcessId,wifiDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "00");
            int updateCount =approveProcessMapper.update(null,updateWrapper);
            //删除流程记录表
            LambdaQueryWrapper<ApproveProcessRecordDO> deleteWrapper = new LambdaQueryWrapper<>();
            deleteWrapper.eq(ApproveProcessRecordDO::getRecordId, wifiDto.getProcessId());
            int deleteCount = approveProcessRecordMapper.delete(deleteWrapper);
            return (updateCount > 0 || deleteCount > 0) ? 1 : 0;
        }
        return result;
    }
}
